package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.exception.ShoppingCartBusinessException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/*
    TODO 注意沉余字段
     neme，image，price，
     前台接口swagger注解都没写
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Override
    public void add(ShoppingCartDTO dto) {

        //判断购物车是菜品还是套餐
        ShoppingCart shoppingCart = null;

        if (null != dto.getDishId()) {
            //提取菜品添加信息
            Long dishId = dto.getDishId();
            Dish dish = dishMapper.getById(dishId);
            shoppingCart = ShoppingCart.builder()
                    .dishId(dishId)
                    .dishFlavor(dto.getDishFlavor())
                    .name(dish.getName())
                    .image(dish.getImage())
                    .amount(dish.getPrice())
                    .build();

        } else if (null != dto.getSetmealId()) {
            //提取套餐添加信息
            Long setmealId = dto.getSetmealId();
            Setmeal setmeal = setmealMapper.getById(setmealId);
            shoppingCart = ShoppingCart.builder()
                    .setmealId(setmealId)
                    .name(setmeal.getName())
                    .image(setmeal.getImage())
                    .amount(setmeal.getPrice())
                    .build();

        } else
            throw new ShoppingCartBusinessException("没有指定要添加的套餐或菜品");


        //购物车唯一根据id，用户id和套餐id或用户id和菜品id和菜品风味确定
        // 没购物车数量默认1，有加1
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        ShoppingCart unique = shoppingCartMapper.getUnique(shoppingCart);

        if (null == unique) {
            shoppingCart.setNumber(1);
            shoppingCartMapper.save(shoppingCart);

        } else {
            shoppingCartMapper.update(
                    ShoppingCart.builder()
                            .id(unique.getId())
                            .number(unique.getNumber() + 1)
                            .build()
            );

        }
    }

    @Override
    public List<ShoppingCart> getByUserId() {

        return shoppingCartMapper.getList(
                ShoppingCart.builder()
                        .userId(BaseContext.getCurrentId())
                        .build()
        );
    }

    @Override
    public void delOne(ShoppingCartDTO dto) {

        ShoppingCart unique = null;
        Long userId = BaseContext.getCurrentId();

        if (null != dto.getDishId()) {
            //提取菜品删除信息
            unique = shoppingCartMapper.getUnique(
                    ShoppingCart.builder()
                            .userId(userId)
                            .dishId(dto.getDishId())
                            .dishFlavor(dto.getDishFlavor())
                            .build()
            );

        } else if (null != dto.getSetmealId()) {
            //提取套餐删除信息
            unique = shoppingCartMapper.getUnique(
                    ShoppingCart.builder()
                            .userId(userId)
                            .setmealId(dto.getSetmealId())
                            .build()
            );

        } else
            throw new ShoppingCartBusinessException("没有指定要删除的套餐或菜品");

        //数量为1删除，数量>1减减
        if (1 == unique.getNumber())
            shoppingCartMapper.delById(unique.getId());

        else {
            shoppingCartMapper.update(
                    ShoppingCart.builder()
                            .id(unique.getId())
                            .number(unique.getNumber() - 1)
                            .build()
            );

        }
    }

    @Override
    public void clear() {
        shoppingCartMapper.delByUserId(BaseContext.getCurrentId());
    }
}
